package aqs;

import java.util.concurrent.locks.ReentrantLock;

//可重入锁 功能2：公平策略
public class Test_fair {
    // 分别测试为 true 和为 false 的输出
    private static final ReentrantLock reentrantLock = new ReentrantLock(true);

    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(() -> {
            test();
        }, "A");
        Thread b = new Thread(() -> {
            test();
        }, "B");
        Thread c = new Thread(() -> {
            test();
        }, "C");

        a.start();
        b.start();
        c.start();
    }

    public static void test() {
        reentrantLock.lock();
        try {
            System.out.println("线程" + Thread.currentThread().getName());
        } finally {
            reentrantLock.unlock();
        }
    }
}
